package littlewhisker.components.ui.misc {
	import flash.display.Sprite;
	import flash.events.IEventDispatcher;
	import littlewhisker.globals.constants.eventtypes.UIEventTypes;

	import flash.events.Event;
	import flash.events.MouseEvent;

	import littlewhisker.components.ui.button.AbstractButton;


	import flash.geom.Rectangle;
	
	/**
	 * @author Philip
	 */
	public class DragableItem extends AbstractButton {
		
		private var _area:Rectangle;
		
		public function get area() : Rectangle {
			return _area;
		}
		
		public function set area(area : Rectangle) : void {
			_area = area;
		}
		
		public function DragableItem(asset:Sprite, area:Rectangle){
			super.asset = asset;
			this.area = area;	
			eventDispatcher ? this.eventDispatcher = asset : this.eventDispatcher = this;	
		}

		override protected function onDown(event:MouseEvent):void{
			asset.startDrag( false, area );
			asset.stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
			asset.addEventListener( Event.ENTER_FRAME, onEnterFrame );
		}
		
		override protected function onUp( event:MouseEvent ):void {
			asset.removeEventListener( Event.ENTER_FRAME, onEnterFrame );	
			asset.stopDrag();
		}
		
		private function onEnterFrame( event:Event ):void {
			dispatchEvent(new Event(UIEventTypes.ON_MOVE));
		}
	}
}
